package com.kitty.game.fight.reward;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.PetSet;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.service.MonsterService;
import com.kitty.game.item.config.ChangeCard;
import com.kitty.game.item.service.ChangeCardService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.utils.Const;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Component
public class MonsterFightRewardHandler extends FightRewardHandler {

    @Override
    public void sendReward(Fight fight, Role role, Pet pet) {
        // 野怪战斗 只给经验和少量 代金券(会员给金钱)
        // 取出petset 计算经验 随机掉落代金券(会员给金钱)
        int exp = (int) (fight.getExpMap() != null ? fight.getExpMap().getInt("exp", 0) / 5 * 0.7 : 0);
        int monsterLevel = fight.getExpMap() != null ? fight.getExpMap().getInt("level", 0) : 0;
        logger.info("获得经验==={}", exp);
        if (exp <= 0) {return ;}
        exp=exp/3;
        RoleService roleService = SpringUtils.getRoleService();
        NutMap nutMap = role.getPropsStatus();
        int point = nutMap.getInt("role");
        if (nutMap.getInt("roleStatus") == 1 && point >= 4) {
            nutMap.setv("role", point - 4);
            role.save();
            roleService.addExp(role, exp * 2, monsterLevel, pet != null ? pet.getId() : 0);// 需要考虑到人物有没有使用双倍经验 怪物等级高于人物十级 不能获得经验
        } else {
            roleService.addExp(role, exp, monsterLevel, pet != null ? pet.getId() : 0);// 需要考虑到人物有没有使用双倍经验 怪物等级高于人物十级 不能获得经验
        }
        randomChangeCard(role,fight.getMapId());
    }

    @Override
    public void fightFailed(Fight fight, Role role) {

    }

    @Override
    public void flee(Fight fight, Role role) {

    }

    @Override
    public int getType() {
        return Const.fightType_monster;
    }


    /**
     * 根据神木鼎点数 和开启状态 随机掉落变身卡
     */
    private void randomChangeCard(Role role ,int mapId) {
       if(SpringUtils.getBean(ChangeCardService.class).getStorePos(role)<=0) return;
        Random random =new Random();
        if(random.nextInt(100)>95){
            return;
        }
        NutMap propsStatus = role.getPropsStatus();
        /**神木鼎开启的情况下才往下执行*/
        if(propsStatus.getInt("shenmuStatus",0)!=1){
           return;
        }
        int shenmu =propsStatus.getInt("shenmu",0);
        if(shenmu<25){
            return;
        }
        propsStatus.setv("shenmu",shenmu-25);
        role.setPropsStatus(propsStatus);

        ArrayList<FiedValue> list = new ArrayList<>();
        FiedValue fiedValue = new FiedValue();
        fiedValue.setType((short) 875);
        fiedValue.setVT((byte) 2);
        fiedValue.setValue((short) propsStatus.getInt("shenmu"));
        list.add(fiedValue);
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);

        /**取出这个地图的野怪*/
        List<PetSet> monsters = SpringUtils.getBean(MonsterService.class).getMapMonster(mapId);
        int icon =monsters.get(random.nextInt(monsters.size())).getIcon();

        ChangeCard changeCard =SpringUtils.getBean(ChangeCardService.class).getCardByIcon(icon);
        if(changeCard==null){
            return;
        }
        SpringUtils.getBean(ChangeCardService.class).getChangeCard(role,changeCard.getName());
        MessagePusher.pushMessage(role,new RespNotifyMiscEx(MessageFormat.format("你消耗#R{0}#n点神木鼎点数，获得了一张{1}，已装入卡套。",25,changeCard.getName())));
    }
}
